package stcu;

import com.nvlled.route.*;
import stcu.robjects.*;
import java.util.*;
import java.net.*;

public class FileInfo {

    private String owner;
    private String filename;
    private long fileSize;
    private InetSocketAddress fileAddress;

    public FileInfo(String filename, String owner, InetSocketAddress sockAddr) {
        this.filename = filename;
        this.owner = owner;
        this.fileAddress = sockAddr;
    }

    public FileInfo(FileAdd file) {
        owner = file.getOwner();
        filename = file.getName();
        fileSize = file.getSize();

        InetAddress address = 
            extractInetAddress(file.getSourceAddress());
        fileAddress = 
            new InetSocketAddress(address, file.getPort());
    }

    private InetAddress extractInetAddress(SocketAddress sockAddress) {
        InetSocketAddress netAddress = (InetSocketAddress) sockAddress;
        return netAddress.getAddress();
    }

    public FileInfo(FileDel file) {
        owner = file.getOwner();
        filename = file.getName();
        fileSize = file.getSize();
    }

    public FileAdd toFileAdd() {
        return new FileAdd(getName(), 
                getOwner(), 
                fileAddress.getPort());
    }

    public FileDel toFileDel() {
        return new FileDel(getName(), getOwner());
    }

    public String getName() {
        return filename;
    }

    public String getOwner() {
        return owner;
    }

    @Override
    public boolean equals(Object obj) {
        FileInfo file = (FileInfo) obj;
        return getName().equals(file.getName()) &&
            getOwner().equals(file.getOwner());
    }

    public InetAddress getAddress() {
        return fileAddress.getAddress();
    }

    public int getPort() {
        return fileAddress.getPort();
    }

    @Override
    public String toString() {
        return getOwner() + ":" + getName();
    }
}

